import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { keyBy, get } from "lodash-es";
import { deleteByIds } from "@/plugins/ai-warning/organization/api/rdOrganizeZone.ts";
import { rdOrganizeZoneStatusDictData } from "./common.tsx";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const rdOrganizeZoneStatusMap = keyBy(
    rdOrganizeZoneStatusDictData(),
    "value"
  );
  const msg = useMessage();

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    { label: "大区名称", prop: "name" },
    {
      label: "状态",
      prop: "status",
      renderProps: { multiple: false },
      cellRender: ({ row }) => {
        return (
          <ElTag type="primary">
            {get(rdOrganizeZoneStatusMap, row.status, { label: "未知" }).label}
          </ElTag>
        );
      },
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      fixed: "right",
      width: 120,
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("organization:rdOrganizeZone:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("organization:rdOrganizeZone:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
